//
//  matchVC.swift
//  Match Egg
//
//  Created by jignesh on 21/09/19.
//  Copyright © 2019 PK Boss. All rights reserved.
//

import UIKit

class matchVC: UIViewController {

    @IBOutlet var img4: UIImageView!
    @IBOutlet var img3: UIImageView!
    @IBOutlet var img2: UIImageView!
    @IBOutlet var img1: UIImageView!
    @IBOutlet var winView: UIView!
    @IBOutlet var imgWin: UIImageView!
    @IBOutlet var imgToy: UIImageView!
    @IBOutlet var lblHint: UILabel!
    
    var imageListArray = [Int]()
    
    
    var allImageArray = [UIImageView]()
    var finalNumber : Int = 0
    var winIndex : Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        allImageArray.append(img1)
        allImageArray.append(img2)
        allImageArray.append(img3)
        allImageArray.append(img4)
        let userdefult = UserDefaults.standard
        if userdefult.value(forKey:"hint") != nil
        {
            lblHint.text = "Hint : \(userdefult.value(forKey:"hint")!)"
        }
        startPlay()
        
        // Do any additional setup after loading the view.
    }
    @IBAction func clickOnHint(_ sender: Any)
    {
        let userdefult = UserDefaults.standard
        if let hint = Int(userdefult.value(forKey:"hint") as! String)
        {
            if hint > 0
            {
                lblHint.text = "Hint : \(hint - 1)"
                userdefult.set("\(hint - 1)", forKey: "hint")
                userdefult.synchronize()
                showHintAnimation()
            }
            else
            {
                let alert = UIAlertController(title: "Alert!", message: "You have not enough hint. to increase hint Please come tommorrow.", preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                }))
                self.present(alert, animated: true, completion: nil)
                return
            }
        }
    }
    
    func showHintAnimation()
    {
        let imagView = allImageArray[winIndex]
        UIView.animate(withDuration: 1, animations: {
            imagView.alpha = 0
        }) { _ in
            imagView.alpha = 1
        }
    }
    
    func startPlay()
    {
        winView.isHidden = true
        let array = NSMutableArray()
        for i in 1..<5
        {
            array.add(i)
        }
        
        print(array)
        
        let a = Int.random(in: 0 ..< array.count)
        img1.image = UIImage.init(named: "egg\(array[a])")
        array.removeObject(at: a)
        
        let b = Int.random(in: 0 ..< array.count)
        img2.image = UIImage.init(named: "egg\(array[b])")
        array.removeObject(at: b)

        
        let c = Int.random(in: 0 ..< array.count)
        img3.image = UIImage.init(named: "egg\(array[c])")
        array.removeObject(at: c)
        
        let d = Int.random(in: 0 ..< array.count)
        img4.image = UIImage.init(named: "egg\(array[d])")
        
        finalNumber = Int.random(in: 1 ..< 23)
        imgToy.image = UIImage.init(named: "\(finalNumber)")
        
        imageListArray = [Int]()
        let win = Int.random(in: 0 ..< 4)
        let toysArray = NSMutableArray()
        for i in 1..<23
        {
            toysArray.add(i)
        }
        toysArray.remove(finalNumber)
        for i in 0..<4
        {
            if win == i
            {
                imageListArray.append(finalNumber)
                winIndex = i
            }
            else
            {
                
                let img = Int.random(in: 0 ..< toysArray.count)
                imageListArray.append(toysArray.object(at: img) as! Int)
                toysArray.removeObject(at: img)
            }
        }
    }
    
    @IBAction func clickOnDismiss(_ sender: Any)
    {
        self.dismiss(animated: false, completion: nil)
    }
    
    @IBAction func clickPlayAgain(_ sender: Any)
    {
        startPlay()
    }
    
    @IBAction func clcikEgg(_ sender: Any)
    {
        let number = (sender as AnyObject).tag
        
        for i in 0..<imageListArray.count
        {
            let imagView = allImageArray[i]
            imagView.image = UIImage.init(named: "\(imageListArray[i])")
        }
        
        
        if number == winIndex + 1
        {
            showWinAnimation(withNumber: number ?? 0, isWin: 1)
        }
        else
        {
            showWinAnimation(withNumber: winIndex + 1, isWin: 0)
        }
        
        
//        let win = Int.random(in: 0 ..< 2)
//        let toysArray = NSMutableArray()
//
//        for i in 1..<23
//        {
//            toysArray.add(i)
//        }
//        toysArray.remove(finalNumber)
//
//        if win == 1
//        {
//            for i in 1..<5
//            {
//                let imagView = allImageArray[i-1]
//                if number == i
//                {
//                   imagView.image = UIImage.init(named: "\(finalNumber)")
//                }
//                else
//                {
//                    let img = Int.random(in: 0 ..< toysArray.count)
//                    imagView.image = UIImage.init(named: "\(toysArray.object(at: img))")
//                    toysArray.removeObject(at: img)
//                }
//            }
//            showWinAnimation(withNumber: number ?? 0, isWin: 1)
//        }
//        else
//        {
//            let winValue = Int.random(in: 0 ..< 3)
//            var winToys : Int = 0
//            for i in 1..<5
//            {
//                let imagView = allImageArray[i-1]
//                if number == i
//                {
//                    let img = Int.random(in: 0 ..< toysArray.count)
//                    imagView.image = UIImage.init(named: "\(toysArray.object(at: img))")
//                    toysArray.removeObject(at: img)
//                }
//                else
//                {
//
//                    if winValue == winToys
//                    {
//                        imagView.image = UIImage.init(named: "\(finalNumber)")
//                        showWinAnimation(withNumber: i, isWin: 0)
//                    }
//                    else
//                    {
//                        let img = Int.random(in: 0 ..< toysArray.count)
//                        imagView.image = UIImage.init(named: "\(toysArray.object(at: img))")
//                        toysArray.removeObject(at: img)
//                    }
//                    winToys = winToys + 1
//                }
//            }
//        }
    }
    
    func showWinAnimation(withNumber : Int , isWin : Int)
    {
        let imagView = allImageArray[withNumber - 1]
        UIView.animate(withDuration: 1, animations: {
            imagView.alpha = 0
        }) { _ in
            imagView.alpha = 1
            self.showWinORLoseScreen(winValue: isWin)
        }
    }
    
    func showWinORLoseScreen(winValue : Int)
    {
        if winValue == 1
        {
            //let winValue = Int.random(in: 0 ..< 3)
            
            DispatchQueue.main.async {
                self.imgWin.image =  UIImage.gifImageWithName("great")
            }
            
            
            winView.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
                
                self.winView.isHidden = true
                 self.startPlay()
            }
        }
        else
        {
           // let winValue = Int.random(in: 0 ..< 3)
            
            DispatchQueue.main.async {
                self.imgWin.image =  UIImage.gifImageWithName("sorry")
            }
            
            
            winView.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
                
                self.winView.isHidden = true
                self.startPlay()
            }
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
